package qlsl.androiddesign.view.subview.fragmentview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.subactivity.PostingDetailActivity;
import qlsl.androiddesign.activity.subactivity.TestVpVpActivity;
import qlsl.androiddesign.adapter.subadapter.PostingCategoryAdapter;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.fragment.commonfragment.TestVp2Fragment;
import qlsl.androiddesign.http.service.subservice.ForumService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 出售页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class TestVp2View extends FunctionView<TestVpVpActivity> {

	protected TestVp2Fragment fragment;

	protected PullToRefreshListView refreshView;

	protected List<Posting> list = new ArrayList<Posting>();

	protected Pager pager;

	public TestVp2View(TestVp2Fragment fragment, ViewGroup rootView,
			ViewGroup contentView) {
		super((TestVpVpActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	protected void initView(View view) {
		hideTitleBar();
		refreshView = findViewById(R.id.refreshListView);
	}

	protected void initData() {
		query(TvView.city);
	}

	public void query(String city) {
		ForumService.queryPostingList(1, "CS", city, this, fragment);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<Posting> list_result = (List<Posting>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list_item:
			doClickListItem(view);
			break;
		}
	}

	private void doClickListItem(View view) {
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		Posting item = (Posting) refreshView.getAdapter().getItem(position);
		Intent intent = new Intent(activity, PostingDetailActivity.class);
		intent.putExtra("data", item);
		startActivityForResult(intent, 0);
	}

	protected void resetList(List<Posting> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	protected void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new PostingCategoryAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	private OnRefreshListener<ListView> refreshListener = new OnRefreshListener<ListView>() {
		@SuppressWarnings("deprecation")
		public void onRefresh(PullToRefreshBase<ListView> refreshView) {
			refreshView.setRefreshingLabel(SoftwareApplication.getInstance()
					.getResources().getString(R.string.refreshing_label));
			refreshView.onLoadComplete();
		}

	};

}
